package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public PageInfo list(Integer page, Integer rows, String customerName) {
        PageHelper.startPage(page,rows);
        List<Customer> list = customerDao.list(customerName);
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void save(Customer customer) {
        if(customer.getCustomerId() == null){
            // 添加客户
            customerDao.addCustomer(customer);
        }else {
            // 修改客户
            customerDao.updateCustomer(customer);
        }
    }

    @Override
    public void delete(String[] ids) {
        if(ids != null && ids.length>0){
            for (String id : ids) {
                customerDao.deleteById(id);
            }
        }
    }
}
